package com.sinodata.bsm.center.web.action;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

/**
 * 
 * <p>
 * Description: Action基类
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-5-9 AM 9:12:40      liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see   
 * @param <T>
 */
public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>, Preparable, ServletRequestAware, ServletResponseAware {

    private static final long serialVersionUID = -1653204626115064950L;

    protected static final Logger logger = Logger.getLogger(BaseAction.class);

    /**
     * HttpServletRequest对象
     */
    protected HttpServletRequest request;

    /**
     * HttpServletResponse对象
     */
    protected HttpServletResponse response;

    /**
     * 设置HttpServletRequest
     */
    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;

    }

    /**
     * 设置HttpServletResponse
     */
    @Override
    public void setServletResponse(HttpServletResponse arg0) {
        response = arg0;
    }

    public BaseAction() {
        super();
    }

    @Override
    public String execute() throws Exception {
        return list();
    }

    public abstract String list() throws Exception;

    /**
     * Action函数,显示新增或修改Entity界面. 建议return INPUT.
     */
    @Override
    public abstract String input() throws Exception;

    /**
     * Action函数,新增或修改Entity. 建议return RELOAD.
     */
    public abstract String save() throws Exception;

    /**
     * Action函数,删除Entity. 建议return RELOAD.
     */
    public abstract String delete() throws Exception;

    // -- Preparable函数 --//
    /**
     * 实现空的prepare()函数,屏蔽了所有Action函数都会执行的公共的二次绑定.
     */
    @Override
    public void prepare() throws Exception {

    }

    /**
     * 定义在input()前执行二次绑定.
     */
    public void prepareInput() throws Exception {
        prepareModel();
    }

    /**
     * 定义在save()前执行二次绑定.
     */
    public void prepareSave() throws Exception {
        prepareModel();
    }

    /**
     * 等同于prepare()的内部函数,供prepardMethodName()函数调用.
     */
    protected abstract void prepareModel() throws Exception;

    /**
     * 向前台写数据
     * 
     * @param text
     */
    protected void renderText(final String text) {
        render(text, "UTF-8");
    }

    /**
     * 转换字符集编码
     *
     * @param text
     * @param contentType
     */
    private void render(final String text, final String contentType) {
        response.setCharacterEncoding(contentType);// 设置字符集编码
        response.setContentType("text/html;charset=" + contentType);// 页面先设置了编码集,用上方法设置response编码集
        try {
            response.getWriter().write(text);
            response.getWriter().flush();
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
    }

}
